package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocal;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 图片上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile multipartFile) {
        log.info("图片信息====》{}", multipartFile);
        try {

            //获取图片名称
            String originalFilename = multipartFile.getOriginalFilename();//获取原始名称
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));//获取后缀名
            String filename = UUID.randomUUID() + suffix;
            //上传图片minio
            String path = fileStorageService.uploadImgFile("", filename, multipartFile.getInputStream());
            // 新增到Wm_material表
            //先获取表
            WmMaterial wmMaterial = new WmMaterial();
            //封装数据
            wmMaterial.setUrl(path);
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            WmUser wmUser = WmThreadLocal.getWmUser();
            wmMaterial.setUserId(wmUser.getId());
            save(wmMaterial);
            return ResponseResult.okResult("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "出现异常开始捕获");
        }


    }


    /**
     * 列表查询
     *
     * @param dto
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {

        //先参数校验
        if (dto.getSize() == null) {
            dto.setSize(10);
        }
        if (dto.getPage() == null) {
            dto.setPage(1);
        }
        //执行分页查询
        IPage<WmMaterial> iPage = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper = Wrappers.lambdaQuery();
        if (dto.getIsCollection() == 1) {
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocal.getWmUser().getId());
        iPage = page(iPage, queryWrapper);
        //封装数据
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;

    }
}